var mask = null;
var loginPerson = new Object();




$(function() { // 初始化内容
	mui.init();
	tap_init();
	mask = mui.createMask(); //遮罩层
	mui.plusReady();

	var username = localStorage.getItem('username')
	$('#account').val(cleanzf(username))
	var password = localStorage.getItem('password')
	$('#password').val(cleanzf(password))
	
	
	// console.log("======"+returnCitySN["cip"]+','+returnCitySN["cname"])

})

function tap_init() {

	jQuery(document).ready(function() {

		jQuery("#role").change(function() {

			var role = $('#role option:selected').val();
			console.log(role)
			loginPerson.role = role;
			window.localStorage.setItem("loginUser", JSON.stringify(loginPerson))
			operIndex(role)
		});

	});


}


function cleanzf(data) {
	data = data.replace("\"", "").replace("\"", "");
	return data
}
//顾问登录
function loginAir() {
	window.localStorage.removeItem('loginUser')
	var account = $('#account').val();
	var password = $('#password').val();
	var data = {
		"userName": account,
		"passwd": password,
		"sb":"移动端"
	}
	window.localStorage.setItem("username", account)

	window.localStorage.setItem("password", password)

	console.log("data====" + JSON.stringify(data));
	jQuery.ajax({
		type: "post",
		url: "http://xlczb.lgdsoft.cn/api/newGetUserToken",
		data: JSON.stringify(data),
		contentType: "application/json;charset=UTF-8",
		dataType: "json",
		timeout: 10000,
		beforeSend: function() {
			//mask.show();//显示遮罩层
		},
		success: function(data) {
			//mask.close();//显示遮罩层
			//console.log("登录返回信息" + JSON.stringify(data));
			var roleList = data.roleList;
			if (data.error == "用户名不存在" || data.error == "密码不正确") {
				mui.alert(data.error);
			} else {
				var role = JSON.parse(roleList);
				var userinfo = data.userinfo;
				if (data.error == undefined) {
					loginPerson.passwd = userinfo.loginpasswd;
					loginPerson.userName = userinfo.loginname;
					loginPerson.name = userinfo.name;
					loginPerson.id = userinfo.id;
					loginPerson.schoolid = userinfo.schoolid;
					loginPerson.schoolname = userinfo.schoolname;
					loginPerson.positionid = userinfo.positionids;
					loginPerson.positionname = userinfo.positions;
					loginPerson.upschoolid = userinfo.upschoolid;
					loginPerson.upschoolname = userinfo.upschoolname;
					loginPerson.token = data.token;


					console.log(role)

					$("#role").find("option").remove();
					if (role.length == 1) {
						operIndex(role[0].name);
						loginPerson.role = role[0].name;
						window.localStorage.setItem("loginUser", JSON.stringify(loginPerson))
					} else {
						setTimeout(function() {
							$("#role").removeAttr("style");
							mui.alert("请点击按钮下面选择权限");

						}, 1000);
						var roleStr = "<option value=\"\">请选择</option>";
						for (var i = 0; i < role.length; i++) {
							roleStr += "<option value=\"" + role[i].name + "\" >" + role[i].description + "</option>";
						}
						$("#role").append(roleStr);
						console.log(roleStr)

					}


				} else {
					mui.alert(data.error);
				}
			}
		},

		error: function(e) {
			console.log(JSON.stringify(e));
		}
	});


}


function operIndex(role) {

	switch (role) {
		case 'ROLE_ZX_JS':
			openTeacherIndex();
			break;
		case 'ROLE_FX_XZ':
			openRectorIndex();
			break;
		case 'ROLE_ZJB':
			openManagerIndex();
			break;
		case 'ROLE_ZX_JL':
			openManagerIndex();
			break;
			
		case 'ROLE_DQ_JL':
			openRegionManagerIndex();
			break;
		case 'ROLE_FX_GW':
			openSalerIndex();
			break;
		case 'ROLE_ZX_PTKF':
			openService();
			break;

		case 'ROLE_KF_ZZ':
			openServiceGroupLeader();
			break;
		default:
			mui.alert('客户端正在构建,请耐心等待');
			break;
	}


	// if(role == 'ROLE_FX_GW' || role == 'ROLE_FX_ZG'){
	// 	openIndex();
	// }else if(role=='ROLE_DQ_CW'){
	// 	openIndex1();
	// }else if(role=='ROLE_FX_CW'){
	// 	openIndex1();
	// }else if(role == 'ROLE_FX_XZ'){
	// 	openIndex2();	
	// }else if(role=='ROLE_DQ_JL'){
	// 	openIndex4();
	// }else if(role=='ROLE_ZX_JL'){
	// 	openIndex5();
	// }else if(role=='ROLE_ZJB'){
	// 	openIndex6();
	// }
	// else {
	// 	mui.alert('无权限访问');
	// }
}


//财务
function openFinanceIndex() {
	setTimeout(function() {
		mui.alert('功能正在构建,请耐心等待');
	}, 300)
	//window.location.href = "html/finance/work.html"
}
//分校校长
function openRectorIndex() {
	window.location.href = "html/rector/work.html"
}
//教师
function openTeacherIndex() {
	window.location.href = "html/teacher/work.html"
}
//大区经理
function openRegionManagerIndex() {
	// setTimeout(function() {
	// 	mui.alert('功能正在构建,请耐心等待');
	// }, 300)
	window.location.href = "html/regionManager/work.html"
}
//总经理
function openManagerIndex() {

	window.location.href = "html/manager/work.html"
}
//营运部
function openOperationIndex() {
	setTimeout(function() {
		mui.alert('功能正在构建,请耐心等待');
	}, 300)
	//window.location.href = "html/operation/work.html"

}

//客服专员
function openService() {
	window.location.href = "html/service/work.html"
}

function openServiceGroupLeader() {
	window.location.href = "html/serviceGroupLeader/work.html"

}
//分销顾问
function openSalerIndex() {
	window.location.href = "html/saler/work.html"
}

function takeLogin(result) {
	if (result.rows.length == 1) {
		var user_json = JSON.parse(result.rows.item(0).value);
		jQuery("#account").val(user_json.userName);
		jQuery("#password").val(user_json.passwd);
		setTimeout(function() {
			loginAir('false');
		}, 300);
		console.log(result.rows.item(0).value);
	}
}


function showSheet(title, btns, func) {
	if (title && btns && btns.length > 0) {
		var btnArray = [];
		for (var i = 0; i < btns.length; i++) {
			btnArray.push({
				title: btns[i]
			});
		}

		plus.nativeUI.actionSheet({
			title: title,
			cancel: '取消',
			buttons: btnArray
		}, function(e) {
			if (func) func(e);
		});
	}
}
